package com.sky.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.result.Result;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController("UserSetmealController")
@RequestMapping("/user/setmeal")
@Slf4j
@Api("套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 根据id分类查找套餐
     * @return
     */
    @ApiOperation("根据id分类查找套餐")
    @GetMapping("/list")
    public Result<List<SetmealVO>> list(Long categoryId){
        //规定key： setmeal_ + categoryId
        String key="setmeal_"+categoryId;
        //查找Redis中是否有缓存
        List<SetmealVO> list = (List<SetmealVO>) redisTemplate.opsForValue().get(key);
        if(list!=null){
            return Result.success(list);
        }
        //缓存中不存在时，查找数据库
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getCategoryId,categoryId);
        lambdaQueryWrapper.eq(Setmeal::getStatus,1);
        List<Setmeal> setmeals = setmealService.list(lambdaQueryWrapper);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        List<SetmealVO> setmealList=new ArrayList<>();
        setmeals.stream().map((setmeal)->{
            Long setmealId = setmeal.getId();
            SetmealVO setmealVO=new SetmealVO();
            BeanUtils.copyProperties(setmeal,setmealVO);
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealId);
            List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishLambdaQueryWrapper);
            setmealVO.setSetmealDishes(setmealDishes);
            setmealList.add(setmealVO);
            return setmeal;
        }).collect(Collectors.toList());
        redisTemplate.opsForValue().set(key,setmealList);
        return Result.success(setmealList);
    }

    /**
     * 根据setmealId查找对应的dish
     */
    @ApiOperation("根据setmealId查找对应的dish")
    @GetMapping("/dish/{id}")
    public Result<List<SetmealDish>> getDishes(@PathVariable Long id){
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(lambdaQueryWrapper);
        return Result.success(list);
    }


}
